package com.interest.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.interest.base.entity.ModelBean;

import java.util.List;

/**
 * 返回值处理工具
 *
 * @author wangliang
 */
public class ResponseUtils {
    public static JSONObject generSuccJson(JSONObject data) {
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", "");
        json.put("data", data);

        return json;
    }

    public static <T> String generateSuccessJsonStr(T value) {
        return generateSuccessJsonStr("result", value);
    }

    public static <T> String generateSuccessJsonStr(String key, T value) {
        JSONObject data = new JSONObject();
        data.put(key, value);
        return ResponseUtils.generSuccJsonStr(data);
    }

    public static String generSuccJsonStr(JSONObject data) {
        return generSuccJson(data).toJSONString();
    }

    public static String generateSuccessJsonStr(List list) {
        JSONObject data = new JSONObject();
        data.put("list", JSONArray.parse(JSON.toJSONString(list)));
        return generSuccJsonStr(data);
    }


    public static JSONObject generFailedJson(int code, String error) {
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("msg", error);
        return json;
    }

    public static String generFailedJsonStr(int code, String error) {
        return generFailedJson(code, error).toJSONString();
    }

    public static String generateSuccessAccountStr(String username, String token) {
        JSONObject data = new JSONObject();
        data.put("username", username);
        data.put("token", token);
        return ResponseUtils.generSuccJsonStr(data);
    }


    public static ModelBean generateSuccess(List list) {
        JSONObject data = new JSONObject();
        data.put("list", JSONArray.parse(JSON.toJSONString(list)));
        return new ModelBean(data);
    }

}
